package com.laobindj.siji.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.utils.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import com.laobindj.siji.R;
import com.laobindj.siji.adapter.RechargeMobileLableAdapter;
import com.laobindj.siji.contants.Constants;
import com.laobindj.siji.listener.ItemClickListener;
import com.laobindj.siji.model.OrderList;
import com.laobindj.siji.model.OtherExpensesBean;
import com.laobindj.siji.model.UserModel;
import com.laobindj.siji.utils.GsonUtil;
import com.laobindj.siji.utils.MD5;
import com.laobindj.siji.utils.SPUtils;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/12/9.
 */

public class OtherExpensesActivity extends BaseActivity {
    ImageView mIVBack;
    private EditText other_expenses_reson_et;
    GridView recharge_mobile_classify_gv;
    @InjectView(R.id.other_expenses_money_et)
    EditText other_expenses_money_et;

    OrderList mOrder;
    OrderList orderInfo;

    private Context mContext;
    private RechargeMobileLableAdapter lableAdapter;
    private List<OtherExpensesBean.DataBean> lableBeans = new ArrayList<OtherExpensesBean.DataBean>();
    String mOtherExpenses = "";

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_other_expenses);

        ButterKnife.inject(this);
        mIVBack = getViewById(R.id.back);
        addView();
        initData();
        initAdapter();
    }

    private void addView() {
//        order_sn = (TextView) findViewById(R.id.order_sn);
//        add_time = (TextView) findViewById(R.id.add_time);
//        uname = (TextView) findViewById(R.id.endorder_uname);
//        mobile = (TextView) findViewById(R.id.mobile);

        other_expenses_reson_et = (EditText) findViewById(R.id.other_expenses_reson_et);
        recharge_mobile_classify_gv = (GridView) findViewById(R.id.recharge_mobile_classify_gv);
    }

    @Override
    protected void setListener() {
        mIVBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    private void initData() {
        Gson gson = new Gson();

        String result = "{\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"id\": 31,\n" +
                "      \"name\": \"过桥费\",\n" +
                "      \"img\": \"https:\\/\\/bucket-12138.oss-cn-hongkong.aliyuncs.com\\/153810454417200004\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 32,\n" +
                "      \"name\": \"加油费\",\n" +
                "      \"img\": \"https:\\/\\/bucket-12138.oss-cn-hongkong.aliyuncs.com\\/153810456102700005\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 33,\n" +
                "      \"name\": \"停车费\",\n" +
                "      \"img\": \"https:\\/\\/bucket-12138.oss-cn-hongkong.aliyuncs.com\\/153810457611200006\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"code\": \"000\"\n" +
                "}";

        OtherExpensesBean data = gson.fromJson(result, OtherExpensesBean.class);
        lableBeans = data.getData();
    }

    private void initAdapter() {
        //商品类别
        lableAdapter = new RechargeMobileLableAdapter(this, lableBeans, "话费");
        recharge_mobile_classify_gv.setAdapter(lableAdapter);

        lableAdapter.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                lableAdapter.setSelection(postion);
                lableAdapter.notifyDataSetChanged();
                OtherExpensesBean.DataBean data = (OtherExpensesBean.DataBean) lableAdapter.getItem(postion);
//                goodsCategoryId = data.getId();
                mOtherExpenses = data.getName();

                other_expenses_reson_et.setText(mOtherExpenses);
            }


            @Override
            public void onItemLongClick(View view, int postion) {
            }

            @Override
            public void onItemSubViewClick(View view, int postion) {
            }
        });
    }


    @Override
    protected void processLogic(Bundle savedInstanceState) {
        mOrder = getIntent().getParcelableExtra("orderinfo");
    }

    private void getOrderInfo() {
        UserModel userModel = (UserModel) SPUtils.readObject(mApp, Constants.USERMODEL);
        mEngine.getOrderInfo(userModel.getSj_id(), mOrder.getOrder_id(), MD5.getMessageDigest((mOrder.getOrder_id() + Constants.BASE_KEY + userModel.getSj_id()).getBytes())).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String str = response.body().string();
                    JSONObject jo = new JSONObject(str);
                    int status = jo.getInt("status");
                    String msg = jo.getString("msg");
                    if (status == 200) {
                        orderInfo = GsonUtil.jsonOrderBean(jo.getString("result"));

                    } else if (status == 201) {
                        showToast("账号不存在");
                    } else if (status == 301) {
                        showToast("订单不存在");
                    } else if (status == 1001) {
                        showToast("请求失败");
                    } else {
                        showToast("请求异常");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    @OnClick(R.id.commit_other_expense)
    public void jumpToChat() {
        Intent intent = new Intent(this, TallyOrderActivity.class);

        Log.d("修改好价格返回", "" + mOtherExpenses + "---" + other_expenses_money_et.getText().toString());

        intent.putExtra(Constants.CONFIG_INTENT_MONEY, other_expenses_money_et.getText().toString());
        intent.putExtra(Constants.CONFIG_INTENT_RESON, other_expenses_reson_et.getText().toString());
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

}
